<template>
  <div class="content">
    <el-dialog title="班级学生" :visible.sync="dialogSutdentVisible" width="800px" >
      <el-table
        v-loading="loading"
        element-loading-text="拼命加载中..."
        element-loading-background="rgba(255, 255, 255, 0.7)"
        ref="multipleTable"
        :data="student"
        border
        stripe
        style="width: 100%"
        @row-click="toggleSelection"
        @selection-change="handleSelectionChange">
        <el-table-column
          prop="name"
          label="姓名"
          sortable
        >
        </el-table-column>
        <el-table-column
          prop="sex"
          label="性别"
          sortable
        >
          <template slot-scope="scope">
            <span v-if="scope.row.sex===1">男</span>
            <span v-else>女</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="schoolno"
          label="学号"
          sortable
        >
        </el-table-column>
        <el-table-column
          prop="idCard"
          label="身份证">
        </el-table-column>
      </el-table>
    </el-dialog>

    <el-dialog title="班级管理" :visible.sync="dialogVisible" width="450px" @close="closeDialog">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm" style="width: 100%">
        <el-form-item label="班级名称" prop="name">
          <el-input v-model="form.name" style="width: 300px" placeholder="如：1班"></el-input>
        </el-form-item>
        <el-form-item label="标识" prop="code">
          <el-input v-model="form.code" style="width: 300px" placeholder="如：01"></el-input>
        </el-form-item>
        <el-form-item label="学年年级" prop="gradeId">
          <el-select v-model="form.gradeId" filterable placeholder="请选择学年年级">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleUpdate(form)" v-if="this.form.id">更新</el-button>
          <el-button type="primary" @click="handleSave(form)" v-else>添加</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-form :inline="true"  class="demo-form-inline">
      <el-form-item>
        <el-input  placeholder="学年名称" v-model="search.year"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input  placeholder="年级名称" v-model="search.gradeName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input  placeholder="班级名称" v-model="search.name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button  @click="getList(search.year,search.gradeName,search.name)">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="dialogVisible = true">新添班级</el-button>
      </el-form-item>
      <el-form-item >
        <el-form-item >
          <el-popconfirm title="这是确定批量删除吗？" @onConfirm="handleDel(null)">
            <template #reference>
              <el-button type="danger" slot="reference" :disabled="delBtlStatu">批量删除</el-button>
            </template>
          </el-popconfirm>
        </el-form-item>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      element-loading-text="拼命加载中..."
      element-loading-background="rgba(255, 255, 255, 0.7)"
      ref="multipleTable"
      :data="table"
      border
      stripe
      style="width: 100%"
      @row-click="toggleSelection"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column
        prop="grade.year"
        label="学年"
        sortable
      >
      </el-table-column>
      <el-table-column
        prop="grade.name"
        label="年级"
        sortable
      >
      </el-table-column>
      <el-table-column
        prop="name"
        label="班级"
        sortable
      >
      </el-table-column>
      <el-table-column
        prop="code"
        label="标识">
      </el-table-column>
      <el-table-column
        prop="studentNum"
        label="班级人数">
      </el-table-column>
      <el-table-column
        label="操作"
        width="200">
        <template slot-scope="scope">
          <el-button  type="text" size="small" @click="handleGetStudents(scope.row.id)">查看班级学生</el-button>
          <el-button  type="text" size="small" @click="handelEdit(scope.row)">编辑</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm title="这是确定删除吗？" @onConfirm="handleDel(scope.row.id)">
            <el-button  type="text" slot="reference" size="small">删除</el-button>
          </el-popconfirm>

        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="current"
      :page-sizes="[5, 10, 20, 30]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import { getList, listAll, save, update, del, getStudents } from '@/api/sysClass/class'

export default {
  name: 'Class',
  data() {
    return {
      loading: true,
      // 根据多选的数量，将其批量按钮可用/不可用进行变化
      multipleSelection: [],
      //年级下拉框回显
      options: [],
      value: '',

      delBtlStatu: true,
      // 分页用到的属性
      total: 0,
      current: 1,
      size: 10,
      //搜索
      search:{
        year:"",
        gradeName:"",
        name:"",
      },
      //表格
      table:[],
      //对话框
      dialogVisible: false,
      dialogSutdentVisible: false,
      //表单
      form:{
        name:'',
        code:'',
        gradeId:'',
      },
      //校验
      rules: {
        name: [
          { required: true, message: '请输入班级名称', trigger: 'blur' },
        ],
        code: [
          { required: true, message: '请输入班级标识', trigger: 'blur' },
        ],
        gradeId: [
          { required: true, message: '请选择学年年级', trigger: 'change' }
        ],
      },
      student:[]
    }
  },
  created() {
    this.getList()
    this.getGrade()
  },
  methods: {

    toggleSelection(row) {
      this.$refs.multipleTable.toggleRowSelection(row);
    },
    // 触发事件为有复选框发生变化，会传递所有勾选的行数据对象数组
    handleSelectionChange(val) {
      this.multipleSelection = val;
      //设置批量删除的按钮的可用/不可用
      this.delBtlStatu = val.length == 0
    },
    //这里是分页
    handleSizeChange(val) {
      this.size = val
      this.getList()

    },
    handleCurrentChange(val) {
      this.current = val
      this.getList()

    },
    getList(){
      getList(this.search.year,this.search.gradeName,this.search.name,this.current,this.size).then(res => {
        this.table = res.data.data.records
        this.size = res.data.data.size
        this.total = res.data.data.total
        this.current = res.data.data.current
        this.loading = false
      })
    },
    getGrade(){
      listAll().then(res=>{
        this.options = res.data.data
      })
    },
    closeDialog(){
      this.form= { name:'', code:'', gradeId:''}
      this.$refs.form.resetFields();
      this.dialogVisible = false

    },
    handleSave(){
      this.$refs.form.validate(valid => {
        if (valid) {
          save(this.form).then(res=>{
            this.dialogVisible = false
            this.$message({
              message: res.data.msg,
              type: 'success'
            })
            this.getList()
          })
        } else {
          return false;
        }
      });
    },
    handleUpdate(){
      this.$refs.form.validate(valid => {
        if (valid) {
          update(this.form).then(res=>{
            this.dialogVisible = false
            this.$message({
              message: res.data.msg,
              type: 'success'
            })
            this.getList()
            this.getGrade()
          })
        } else {
          return false;
        }
      });
    },
    handelEdit(row){
      this.dialogVisible = true
      this.$nextTick(() => {
        this.form = JSON.parse(JSON.stringify(row))
      })
    },
    handleDel(id){
      const ids = []
      if (id) {
        ids.push(id)
      } else {
        this.multipleSelection.forEach(row => {
          ids.push(row.id)
        })
      }

      del(ids).then(res => {
        this.$message({
          message: res.data.msg,
          type: 'success'
        });
        this.getList()
      })
    },
    handleGetStudents(id){
      getStudents(id).then(res => {
        this.student = res.data.data
        this.$nextTick(() => {
          this.dialogSutdentVisible = true
          this.loading = false
        })

      })
    }
  }
}
</script>

<style scoped>

</style>
